import axios from 'axios'
import querystring from 'querystring'

const errorHandler = (status,info) => {
    switch(status){
        case 400:
            console.log("语义有误");
            break;
        case 401:
            console.log("服务器认证失败");
            break;
        case 403:
            console.log("服务器拒绝访问");
            break;
        case 404:
            console.log("地址错误");
            break;
        case 408:
            console.log("请求超时");
            break;
        case 500:
            console.log("服务器内部错误");
            break;
        case 501:
            console.log("服务器不支持请求的功能，无法完成请求");
            break;
        case 502:
            console.log("服务器无响应");
            break;
            default:
                console.log("未知错误");
                break;
    }
}
const instance = axios.create({
    //网络请求公共配置
    timeout: 5000,
})

//发送数据前的拦截器
instance.interceptors.request.use(
    //config:包含网络请求所有信息
    config => {
        if(config.method === 'post'){
            config.data = querystring.stringify(config.data)
        }
        return config;
    },
    error => {
        return Promise.reject(error)
    }
)
//获取数据前的拦截器
instance.interceptors.response.use(
    response => {
        return response.status === 200 ? Promise.resolve(response) : Promise.reject(response)
    },
    error => {
        errorHandler (response.status,response.info)
    }
)

export default instance;